package com.mq.rabbitmq.srcdemo;

/**
 * Created by Administrator on 2017/8/1.
 */

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 10  * 发送消息
 * 11  * @author zhoufeng
 * 12  *
 * 13
 */
public class Sender03 {
    public static void main(String[] args) throws IOException {
        ConnectionFactory connFac = new ConnectionFactory();
        //RabbitMQ-Server安装在本机，所以直接用127.0.0.1
        connFac.setHost("127.0.0.1");

        //创建一个连接
        Connection conn = null;
        try {
            System.out.println("s23232323");
            conn = connFac.newConnection();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //创建一个渠道

        //定义Queue名称

        //为channel定义queue的属性，queueName为Queue名称
//        channel.queueDeclare(queueName, true, false, false, null);
//        Map<String, Object>  argss = new HashMap<String, Object>();
//        argss.put("vhost", "/");
//        argss.put("username","root");
//        argss.put("password", "root");
//        argss.put("x-message-ttl",6000);
//        argss.put("x-dead-letter-exchange","exchange.dlx.test");
//        argss.put("x-dead-letter-routing-key","queue.dlx.test");
       // channel.queueDeclare("queue.dlx.test", durable, exclusive, autoDelete, argss);

        for(int i =0;i<10000;i++){
            Channel channel = conn.createChannel();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    String msg = "Hello World!";
                    //发送消息
                    String queueName = "channel01";
                    try {
                        channel.basicPublish("", queueName, null, msg.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("send message[" + msg + "] to " + queueName + " success!");
                }
            }).start();

        }


//        try {
//            channel.close();
//        } catch (TimeoutException e) {
//            e.printStackTrace();
//        }
//        conn.close();

    }

}